﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using Model;
using MyBind;

public partial class Communicate_BBS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            bindDList();
            //如果是修改信息则将原信息显示到控件中,保存至viewsate
            if (Request.QueryString["id"] != null)
            {
                string secId = Request.QueryString["id"];
                DAL.bbsSection dal = new DAL.bbsSection();
                Model.bbsSection obj = dal.GetModel(int.Parse(secId));
                this.txtName.Text = obj.name;
                txtDepict.Text = obj.depict;
                string cataId=obj.cataId.ToString();
                ViewState["id"] = obj.id;
                ViewState["originallyName"] = obj.name;
                ViewState["cataId"] = cataId;
                //显示所属类别项
                ListItem item = this.DropDownList1.Items.FindByValue(cataId);
                int index=this.DropDownList1.Items.IndexOf(item);
                this.DropDownList1.SelectedIndex = index;
            }
        }
    }

    //下拉列表bind
    protected void bindDList()
    {
        DAL.bbsCatalog cata = new DAL.bbsCatalog();
        DataTable dt = cata.GetList("").Tables[0];
        this.DropDownList1.DataSource = dt;
        this.DropDownList1.DataTextField = "name";
        this.DropDownList1.DataValueField = "id";
        this.DropDownList1.DataBind();
        ListItem item = new ListItem();
        item.Text = "--请选择所属类别--";
        this.DropDownList1.Items.Insert(0, item);
    }


    protected void imbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("BBSSecList.aspx");
    }


    //添加/更新
    protected void imbSave_Click(object sender, ImageClickEventArgs e)
    {
        DAL.bbsSection dal = new DAL.bbsSection();
        string currentName = this.txtName.Text.Trim();
        string depict = this.txtDepict.Text.Trim();
        Model.bbsSection obj = new Model.bbsSection();
        
         if (currentName.Equals(""))
        {
            this.lblMsg.Text = "类名不能为空！";
        }
        //---------------------------更新
        else if (ViewState["id"] != null)
        {
            int cataId=0;
            if (ViewState["cataId"] != null)
            {
                cataId = int.Parse(ViewState["cataId"].ToString());
            }
            int id = int.Parse(ViewState["id"].ToString());
            string originallyName = ViewState["originallyName"].ToString();
            //名称不存在或者未改变则进行添加
            if (!dal.Exists(currentName) || currentName.Equals(originallyName))
            {
                obj.id = id;
                obj.name = currentName;
                obj.depict = depict;
                obj.cataId = cataId;
                //进行修改
                if (dal.Update(obj) > 0)
                {
                    WriteScript.Mesg(this.Response, "修改成功！");
                    WriteScript.ChangeLoca(this.Response, "BBSSecList.aspx");
                }
                else
                {
                    WriteScript.Mesg(this.Response, "修改失败！");
                }
            }
            else
            {
                this.lblMsg.Text = "已存在同名的版块，请重新指定名称！";
            }
        }
        //---------------------------添加
        else
        {
            if (ViewState["cataId"] == null)
            {
                this.lblMsg.Text = "请选择类别！";
            }
            else
            {
                int cataId = int.Parse(ViewState["cataId"].ToString());
                if (dal.Exists(currentName))
                {
                    this.lblMsg.Text = "已存在同名的版块，请重新指定名称！";
                }
                else
                {
                    obj.name = currentName;
                    obj.depict = depict;
                    obj.cataId = cataId;
                    //进行添加
                    if (dal.Add(obj) > 0)
                    {
                        WriteScript.Mesg(this.Response, "添加成功！");
                        WriteScript.ChangeLoca(this.Response, "BBSSecList.aspx");
                    }
                    else
                    {
                        WriteScript.Mesg(this.Response, "添加失败！");
                    }
                }
            }
        }
    }
    //获取类别
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(this.DropDownList1.SelectedIndex>0){
        string cataId=this.DropDownList1.SelectedValue;
        ViewState["cataId"] = cataId;
        }
    }

    //新建类别
    protected void btnAddCat_Click(object sender, EventArgs e)
    {
        Response.Redirect("BBSCatMangr.aspx?from=BBSSecMangr");
    }
}
